package com.ruoyi.shop.member.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.member.domain.ShopMember;
import com.ruoyi.shop.member.mapper.ShopMemberMapper;
import com.ruoyi.shop.member.service.IShopMemberService;
import cn.hutool.core.util.RandomUtil;

/**
 * 会员用户Service业务层处理
 * 
 * @author xuxiang
 * @date 2022-10-21
 */
@Service
public class ShopMemberServiceImpl implements IShopMemberService {

	@Autowired
	private ShopMemberMapper shopMemberMapper;

	/**
	 * 查询会员用户
	 * 
	 * @param memberId 会员用户主键 整型
	 * @return 会员用户
	 */
	@Override
	public ShopMember selectMemberById(Long memberId) {
		return shopMemberMapper.selectMemberById(memberId);
	}

	/**
	 * 查询会员用户列表
	 * 
	 * @param shopMember 会员用户
	 * @return 会员用户
	 */
	@Override
	public List<ShopMember> selectMemberList(ShopMember shopMember) {
		return shopMemberMapper.selectMemberList(shopMember);
	}

	/**
	 * 新增会员用户
	 * 
	 * @param shopMember 会员用户
	 * @return 结果
	 */
	@Override
	public int insertMember(ShopMember shopMember) {
		return shopMemberMapper.insertMember(shopMember);
	}

	/**
	 * 修改会员用户
	 * 
	 * @param shopMember 会员用户
	 * @return 结果
	 */
	@Override
	public int updateMember(ShopMember shopMember) {
		return shopMemberMapper.updateMember(shopMember);
	}

	/**
	 * 删除会员用户信息
	 * 
	 * @param memberId 会员用户主键
	 * @return 结果
	 */
	@Override
	public int deleteMemberById(Long memberId) {
		return shopMemberMapper.deleteMemberById(memberId);
	}

	/**
	 * 通过账号(登录账号或手机号)查询会员用户
	 * 
	 * @param userName 账号
	 * @return 会员用户
	 */
	public ShopMember selectMemberByUserName(String userName) {
		return shopMemberMapper.selectMemberByUserName(userName);
	}

	/**
	 * 通过登录账号查询会员
	 * 
	 * @param loginName 登录账号
	 * @return 会员用户
	 */
	public ShopMember selectMemberByLoginName(String loginName) {
		return shopMemberMapper.selectMemberByLoginName(loginName);
	}

	/**
	 * 通过手机号查询会员
	 * 
	 * @param mobile 手机号
	 * @return 会员用户
	 */
	@Override
	public ShopMember selectMemberByMobile(String mobile) {
		return shopMemberMapper.selectMemberByMobile(mobile);
	}

	/**
	 * 校验手机号是否唯一
	 *
	 * @param member 会员用户
	 * @return 结果
	 */
	@Override
	public boolean checkMobileUnique(ShopMember member) {
		Long memberId = StringUtils.isNull(member.getMemberId()) ? -1L : member.getMemberId();
		ShopMember info = shopMemberMapper.selectMemberByMobile(member.getMobile());
		if (StringUtils.isNotNull(info) && info.getMemberId().longValue() != memberId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public String checkEmailUnique(ShopMember member) {
		return null;
	}

	/**
	 * 修改用户基本信息
	 * 
	 * @param member 会员用户
	 * @return 结果
	 */
	@Override
	public int updateMemberProfile(ShopMember member) {
		return shopMemberMapper.updateMember(member);
	}

	@Override
	public int resetPwd(ShopMember member) {
		return 0;
	}

	/**
	 * 重置用户密码
	 * 
	 * @param memberId
	 * @param password
	 * @return
	 */
	@Override
	public int resetPwd(Long memberId, String password) {
		return shopMemberMapper.resetPwd(memberId, password);
	}

	/**
	 * 修改用户头像
	 * 
	 * @param userName 账号
	 * @param avatar   头像地址
	 * @return 结果
	 */
	@Override
	public boolean updateAvatar(String memberName, String avatar) {
		return false;
	}

	/**
	 * 注册用户信息
	 * 
	 * @param member 用户信息
	 * @return 结果
	 */
	@Override
	public boolean registerMember(ShopMember member) {
		return shopMemberMapper.insertMember(member) > 0;
	}

	/**
	 * 自动生成会员账号
	 * 
	 * 二开可以自定义规则
	 * 
	 * @return
	 */
	@Override
	public String generateLoginName() {
		return String.valueOf(RandomUtil.randomLong(0, 99999999));
	}

	/**
	 * 绑定手机号
	 * 
	 * @param memberId
	 * @param mobile
	 * @return
	 */
	@Override
	public int bindMobile(Long memberId, String mobile) {
		ShopMember member = new ShopMember();
		member.setMemberId(memberId);
		member.setMobile(mobile);
		return shopMemberMapper.updateMember(member);
	}

}
